package com.java2020.online_mall.service;

import com.java2020.online_mall.dao.GoodsDao;
import com.java2020.online_mall.dao.GoodsDaoImpl;
import com.java2020.online_mall.model.Goods;
import com.java2020.online_mall.model.GoodsParam;
import com.java2020.online_mall.model.GoodsSpec;
import com.java2020.online_mall.model.GoodsType;

import java.util.List;
import java.util.Map;

/**
 * @PackgeName: com.java2020.online_mall.service
 * @Author: MNHK
 * Date: 2020/5/3 10:31
 * @Version:1.8
 */

public class GoodsServiceImpl implements GoodsService{

    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public void addGoods(GoodsParam goodsParam) {
        List<GoodsSpec> specList = goodsParam.getSpecList();
        //规格中的最小值
        double price = specList.get(0).getUnitPrice();
        //取最大值
        int stockNum = specList.get(0).getStockNum();
        for (int i = 1; i < specList.size(); i++) {
            if(price > specList.get(i).getUnitPrice()){
                price = specList.get(i).getUnitPrice();
            }
            if(stockNum < specList.get(i).getStockNum()){
                stockNum = specList.get(i).getStockNum();
            }
        }
        goodsParam.setPrice(price);
        goodsParam.setStockNum(stockNum);
        int goodsId = goodsDao.addGoods(goodsParam);
        goodsDao.addSpecList(goodsId, specList);
    }

    @Override
    public int addSpec(GoodsSpec goodsSpec) {
        return goodsDao.addSpec(goodsSpec);
    }

    @Override
    public int addType(GoodsType goodsType) {
        return goodsDao.addType(goodsType);
    }

    @Override
    public int deleteSpec(GoodsSpec goodsSpec) {
        return goodsDao.deleteSpec(goodsSpec);
    }

    @Override
    public int updateGoods(GoodsParam goodsParam) {
        return goodsDao.updateGoods(goodsParam);
    }

    @Override
    public Map<String, Object> getGoodsInfo(String id) {
        return goodsDao.getGoodsInfo(id);
    }

    @Override
    public int deleteType(String id) {
        return goodsDao.deleteType(id);
    }

    @Override
    public int deleteGoods(String goodsId) {
        return goodsDao.deleteGoods(goodsId);
    }

    @Override
    public List<Goods> getGoodsByType(String typeId) {
        return goodsDao.getGoodsByType(typeId);
    }

    @Override
    public List<GoodsType> getType() {
        return goodsDao.getType();
    }
}
